package com.example.homework_03;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.DialogFragment;

//单择对话框
public class SingleChoiceDialog extends DialogFragment {
    private String[] color = {"red", "blue", "yello"};
    private SingleChoiceListener listener;
    private String value  ;

    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()) ;
        builder.setTitle("选择对话框")
                .setSingleChoiceItems(color, 1, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        value = color[which] ;
                    }
                }).setPositiveButton("确定", new DialogInterface.OnClickListener(){

            @Override
            public void onClick(DialogInterface dialog, int which) {
                listener.onClicked(value);
            }
        });
        return builder.create() ;
    }

    SingleChoiceDialog(Context context, SingleChoiceListener listener) {
        setListener(listener);
    }

    public interface SingleChoiceListener {
        public void onClicked(String choice);
    }

    public void setListener(SingleChoiceListener listener) {
        this.listener = listener;
    }
}
